name: Ci-Cd-Pipeline-to-AWS

env:
  EB_PACKAGE_S3_BUCKET_NAME: "crystall-fastapi-app-packages"  # Corrected spelling
  EB_APPLICATION_NAME: "FastApiCr"
  EB_ENVIROMENT_NAME:  "FastApiCr-env-1"
  DEPLOY_PACKAGE_NAME:  "CrystalPuzzles_${{ github.sha }}.zip"
  AWS_REGION_NAME: "eu-central-1"

on:
  push:
    branches:
      - prod  # Correct trigger syntax      

jobs:
  
  integration:
    runs-on: ubuntu-latest
    # needs: [tests]

    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Create ZIP deployment package
        run: zip -r ${{ env.DEPLOY_PACKAGE_NAME }} ./ -x *.git*

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v3
        with:
          aws-access-key-id: ${{ secrets.MY_AWS_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.MY_AWS_SECRET_KEY }}
          aws-region: ${{ env.AWS_REGION_NAME }}

      - name: Debug environment variables
        run: echo ${{ env.EB_PACKAGE_S3_BUCKET_NAME }}

      - name: Copy Deployment package to S3 bucket
        run: aws s3 cp ${{ env.DEPLOY_PACKAGE_NAME }} s3://${{ env.EB_PACKAGE_S3_BUCKET_NAME }}/

      - name: Print message for task end
        run: echo "Task for S3 complete"


  # deployment:
  #   runs-on: ubuntu-latest
  #   needs: [integration]

  #   steps:
  #     - name: Configure AWS Credentials
  #       uses: aws-actions/configure-aws-credentials@v3
  #       with:
  #         aws-access-key-id: ${{ secrets.MY_AWS_ACCESS_KEY }}
  #         aws-secret-access-key: ${{ secrets.MY_AWS_SECRET_KEY }}
  #         aws-region: ${{ env.AWS_REGION_NAME }}

  #     - name: Create new Elastic Beanstalk Application Version
  #       run: |
  #         aws elasticbeanstalk create-application-version \
  #           --application-name ${{ env.EB_APPLICATION_NAME }} \
  #           --version-label "Ver-${{ github.sha }}" \
  #           --source-bundle S3Bucket=${{ env.EB_PACKAGE_S3_BUCKET_NAME }},S3Key=${{ env.DEPLOY_PACKAGE_NAME }}

  #     - name: Update Elastic Beanstalk Environment Configuration
  #       run: |
  #         aws elasticbeanstalk update-environment \
  #           --environment-name ${{ env.EB_ENVIROMENT_NAME }} \
  #           --option-settings Namespace=aws:autoscaling:launchconfiguration,OptionName=InstanceType,Value=t4g.medium \
  #           --version-label "Ver-${{ github.sha }}"

  #     - name: Print message for task end
  #       run: echo "The deployment task is complete"
